package com.zqh.irmadscore.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zqh.irmadscore.service.PredictService;
import com.zqh.irmadscore.mapper.PredictMapper;
import com.zqh.irmadscore.model.entity.PredictData;
import org.springframework.stereotype.Service;


import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PredictServiceImpl extends ServiceImpl<PredictMapper, PredictData> implements PredictService {


    @Override
    public void predict(List<Double> list) {
        try {
            HttpClient client = HttpClient.newHttpClient();
            ObjectMapper objectMapper = new ObjectMapper();

            // 构造请求体
            Map<String, Object> requestBody = new HashMap<>();
            requestBody.put("values", list);
            String jsonBody = objectMapper.writeValueAsString(requestBody);

            System.out.println("Sending JSON: " + jsonBody);

            // 构建请求
            HttpRequest request = HttpRequest.newBuilder()
                    .uri(URI.create("http://127.0.0.1:8000/predict"))
                    .header("Content-Type", "application/json")
                    .POST(HttpRequest.BodyPublishers.ofString(jsonBody))
                    .build();


            // 发送请求并处理响应
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
            System.out.println("Response Code: " + response.statusCode());
            System.out.println("Response Body: " + response.body());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
